package com.example.administrator.testproject.download.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.example.administrator.testproject.R;
import com.example.administrator.testproject.download.DownState;
import com.example.administrator.testproject.download.HttpLiteDownManager;
import com.example.administrator.testproject.download.bean.LiteOrmDownInfo;
import com.example.administrator.testproject.download.listener.HttpDownOnNextListener;
import com.example.administrator.testproject.download.view.RoundProgressBar;

import java.util.List;

/**
 * Created by shengfh on 2017/12/26.
 */

public class RecyclerProgressAdapter extends RecyclerView.Adapter<RecyclerProgressAdapter.ViewHolder> {
    private Context mContent;
    private List<LiteOrmDownInfo> mListData;
    private HttpLiteDownManager manager;
    public RecyclerProgressAdapter(Context context, List<LiteOrmDownInfo> mListData) {
        this.mContent=context;
        this.mListData = mListData;
        manager= HttpLiteDownManager.getInstance();
    }

    @Override
    public RecyclerProgressAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecyclerProgressAdapter.ViewHolder(LayoutInflater.from(mContent).inflate(R.layout.view_item_progress_holder,null));
    }

    @Override
    public void onBindViewHolder(final RecyclerProgressAdapter.ViewHolder holder, int position) {
        final LiteOrmDownInfo downInfo = mListData.get(position);
//        holder.roundProgressBar.setMax(113);
//        holder.roundProgressBar.setStatus(RoundProgressBar.STATUS.START);
//
//        new Timer().schedule(new TimerTask() {
//            int progress=0;
//            @Override
//            public void run() {
////                projressBar.setMax(11307617);
//                progress+=5;
////                Log.e("Tag","progress"+progress);
//                holder.roundProgressBar.setProgress(progress);
//            }
//        }, 1200, 600);
        holder.roundProgressBar.setMax(1000000);
//        holder.roundProgressBar.setProgress(0);
        Log.e("Tag", downInfo.getCountLength()+"--"+downInfo.getReadLength());
//        currentState(downInfo, holder);
        setListener(downInfo, holder);
                /*第一次恢复 */
        switch (downInfo.getState()){
            case START:
                /*起始状态*/
                holder.roundProgressBar.setVisibility(View.VISIBLE);
                break;
            case PAUSE:
                holder.roundProgressBar.setVisibility(View.VISIBLE);
                holder.roundProgressBar.setStatus(RoundProgressBar.STATUS.PAUSE);
                Log.e("Tag","初始-暂停中");
                break;
            case DOWN:
                holder.roundProgressBar.setVisibility(View.VISIBLE);
                holder.roundProgressBar.setStatus(RoundProgressBar.STATUS.DOWN);
                Log.e("Tag","初始-下载中");
                manager.startDown(downInfo);
                break;
            case STOP:
                holder.roundProgressBar.setVisibility(View.VISIBLE);
                Log.e("Tag","初始-下载停止");
                holder.roundProgressBar.setStatus(RoundProgressBar.STATUS.PAUSE);
                break;
            case ERROR:
                holder.roundProgressBar.setVisibility(View.VISIBLE);
                Log.e("Tag","初始-下載錯誤");
                holder.roundProgressBar.setStatus(RoundProgressBar.STATUS.PAUSE);
                break;
            case  FINISH:
                Log.e("Tag","初始-下载完成");
                holder.roundProgressBar.setVisibility(View.INVISIBLE);
                break;
        }
        holder.roundProgressBar.setOnPasueListener(new RoundProgressBar.OnCheckListener() {

            @Override
            public void onClickStatus(int status) {
                switch (status){
                    case RoundProgressBar.STATUS.START:
                    case RoundProgressBar.STATUS.PAUSE:
                        if(downInfo.getState()!= DownState.FINISH ){
                            manager.startDown(downInfo);
                        }
                        break;
                    case RoundProgressBar.STATUS.DOWN:
                        manager.pause(downInfo);
                        break;
                }

            }
        });
    }
    private void setListener(LiteOrmDownInfo downInfo , final RecyclerProgressAdapter.ViewHolder holder){
        downInfo.setListener(new HttpDownOnNextListener<LiteOrmDownInfo>() {
            @Override
            public void onNext(LiteOrmDownInfo baseDownEntity) {
                Log.e("Tag","提示：下载完成");
                holder.roundProgressBar.setVisibility(View.INVISIBLE);
//                GlideUtils.loadImageView(mContext,R.mipmap.ic_ashcan,holder.downStatusImg);
            }

            @Override
            public void onStart() {
                Log.e("Tag","提示:开始下载"+holder.roundProgressBar);
                holder.roundProgressBar.setStatus(RoundProgressBar.STATUS.DOWN);

            }

            @Override
            public void onComplete() {
                Log.e("Tag","提示:下载结束");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                Log.e("Tag","失败"+e.getMessage());
            }


            @Override
            public void onPuase() {
                super.onPuase();
                Log.e("Tag","提示:暂停");
//                holder.roundProgressBar.setStatus(RoundProgressBar.STATUS.PAUSE);
            }

            @Override
            public void onStop() {
                super.onStop();
            }

            @Override
            public void updateProgress(long readLength, long countLength) {
//                Log.e("Tag","提示:下载中"+"readLength--"+readLength+"--countLength--"+countLength);
                holder.roundProgressBar.setMax((int) countLength);
                holder.roundProgressBar.setProgress((int) readLength);
            }
        });
    }
    @Override
    public int getItemCount() {
        return mListData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private NumberProgressBar progressBar;
        private RoundProgressBar roundProgressBar;
        public ViewHolder(View view) {
            super(view);
            roundProgressBar=(RoundProgressBar)view.findViewById(R.id.pb_download_icon);
            progressBar = (NumberProgressBar)view.findViewById(R.id.number_progress_bar);
        }
    }
}
